// pages/circle/details.js
const app = new getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    postId:0,
    circle_detail:{},
    iszk:false,
    evaluatelist:[],
    comment_num:0,
    img_height:'930rpx',
    item_height: '998rpx',
    evaluetanum:0,
    iscontent:true,
    content:'',
    circleid:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log(options);
    this.setData({
      circleid:options.id
    })
    //获取圈子详细内容
    this.getDetails(options.id);
      //获取评论内容
      this.getEvaluate(options.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //获取评论内容
  getEvaluate: function (options){
    var that = this;
    var userid = app.globalData.openid;
    wx.request({
      url: app.globalData.serverUrl + 'evaluateinfo',
      data: {
        id: options,
        userid: userid
      },
      success: function (res) {
        if(res.data.status == 0){
          that.setData({
            evaluatelist: [],
            evaluetanum: res.data.evaluetanum,
            is_no: false
          })
        }else{
          that.setData({
            evaluatelist: res.data.evaluateinfo,
            evaluetanum: res.data.evaluetanum,
            is_no:true
          })
        }
        //console.log(res);
      }
    })
  },
  //获取圈子详细信息
  getDetails: function (options){
    var that = this;
    var userid = app.globalData.openid;
    wx.request({
      url: app.globalData.serverUrl + 'detailsinfo',
      data: {
        id: options,
        userid:userid
      },
      success: function (res) {
        //console.log(res);
        that.setData({
          circle_detail: res.data
        });
      },
    })
  },
  //评论内容
  input: function (e) {
    if (e.detail.value){
      this.setData({
        content: e.detail.value,
        iscontent: false
      })
    }else{
      this.setData({
        iscontent: true
      })
    }
    
  },
  //发布
  send:function(){
    var that = this;
    var content = that.data.content;
    var circleid = that.data.circleid;
    var userid = app.globalData.openid;
    wx.request({
      url: app.globalData.serverUrl + 'evaluateadd',
      data: {
        userid:userid,
        circleid:circleid,
        content:content
      },
      success: function(res) {
        //console.log(res);
        if (res.data.status==1){
            wx.showToast({
              title: '发布成功',
              icon: 'success',
              duration: 2000,
              success:function(e){
                //console.log(circleid);
                that.getEvaluate(circleid)
              }
            })
        }else if(res.data.status==2){
          wx.showToast({
            title: '请先登录',
            icon: 'none',
            duration: 2000
          })
        }else{
          wx.showToast({
            title: '发布失败，请重新尝试',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  //删除圈子
  delcircle:function(e){
    wx.showModal({
      title: '提示',
      content: '是否删除此圈子',
      showCancel: true,
      success: function(res) {
        if(res.confirm){
          var circleid = e.currentTarget.dataset.postid;
          wx.request({
            url: app.globalData.serverUrl + 'delcircle',
            data: {
              circleid: circleid
            },
            success: function (res) {
              if (res.data.status == 1) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 2000,
                  success: function (res) {
                    // console.log(res);
                    setTimeout(function () {
                      wx.switchTab({
                        url: '/pages/circle/index',
                        success: function (e) {
                          // console.log(e);
                          var page = getCurrentPages().pop();
                          if (page == undefined || page == null) return;
                          page.onLoad();
                        }
                      })
                    }, 2000);
                  }
                })
              } else {
                wx.showToast({
                  title: '删除失败',
                  icon:"none",
                  duration:2000
                })
              }
            }
          })
        }
      }
    })
  },
  //删除评论
  delevaluate:function(e){
    var that = this;
    var evaluateid = e.currentTarget.dataset.postid;
    var circleid = that.data.circleid;
    wx.request({
      url: app.globalData.serverUrl + 'delevaluate',
      data: {
        evaluateid: evaluateid
      },
      success: function(res) {
        if(res.data.status == 1){
          wx.showToast({
            title: '删除成功',
            icon: 'success',
            duration: 2000
          })
          that.getEvaluate(circleid);
        }else{
          wx.showToast({
            title: '删除失败',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 关注
  click_gz:function(){
    var that = this;
    var circleid = that.data.circleid;
    var userid = app.globalData.openid;
    wx.request({
      url: app.globalData.serverUrl + 'focusadd',
      data: {
        circleid:circleid,
        userid:userid
      },
      success: function(res) {
        if(res.data.status ==1){
          wx.showToast({
            title: 'ok~',
            icon: 'success',
            mask: true,
            success: function (e) {
              that.getDetails(circleid);
            }
          })
        } else if (res.data.status == 2){
          wx.showToast({
            title: '请先登录',
            icon: 'none',
            duration: 2000
          })
        }else{
          wx.showToast({
            title: '点击失败，请重新尝试',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  //增加爱心
  onHeart: function (e) {
    var postId = e.currentTarget.dataset.postid;
    var user_id = app.globalData.openid
    var that = this;
    var circleid = that.data.circleid;
    wx.request({
      url: app.globalData.serverUrl + 'heartadds',
      data: {
        id: postId,
        userid: user_id
      },
      success: function (res) {
        //console.log(res);
        if (res.data.status == 1) {
          that.setData({
            heart_num: res.data.num,
            is_gz: res.data.is_gz
          }),
            wx.showToast({
              title: 'ok~',
              icon: 'success',
              mask: true,
              success: function (e) {
                that.getDetails(circleid);
              }
            })
        } else if (res.data.status == 2) {
          wx.showToast({
            title: '请先登录',
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '点击失败，请重新尝试',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 展开收起
  contentzk:function(e){
    // console.log(e);
    if(!this.data.iszk){
      this.setData({
        iszk:true
      });
    }else{
      this.setData({
        iszk: false
      });
    }
  },
  //评论跳转
  onTapevalueate:function(e){
    var postId = e.currentTarget.dataset.postid;
    //console.log(postId);
    wx.navigateTo({
      url: '/pages/circle/evaluatelist?id='+postId,
    })
  }
})